package aicue1.services;

import aicue1.beans.Customer;
import aicue1.singletons.CustomersSingleton;
import aicue1.soapfaults.MessageTooLongFault;
import aicue1.soapfaults.UnknownCustomerFault;
import java.util.Date;
import java.util.Random;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/*@WebService(name = "NotificationService",
            targetNamespace = "http://infosys.tuwien.ac.at/ait09/ass1/dto/notification",
            portName = "NotificationPT")*/
public class SMSService implements NotificationService
{
    @WebMethod(operationName="notify_customer")
    public boolean notify_customer( @WebParam(name = "customer") Customer customer, 
            @WebParam(name = "message") String message) throws UnknownCustomerFault, MessageTooLongFault
    {
        Boolean notified = false;
        CustomersSingleton customersInstance = CustomersSingleton.getInstance();

        System.out.println("\n[SMSService] " + new Date() + " :: " + customer.getId());
        System.out.println("[SMSService] Sending message :: " + message);

        if(!customersInstance.doesCustomerExist(customer))
        {
            System.out.println("[SMSService] Success :: false (User doesn't exist)");
            throw new UnknownCustomerFault("User doesn't exist.");

        }
        else if(message.length() > 160)
        {
            System.out.println("[SMSService] Success :: false (Message is too long)");
            throw new MessageTooLongFault("Message is too long (max. 160 characters).");
        }
        else
        {
            Random random = new Random();
            notified = random.nextBoolean();
            System.out.println("[SMSService] Success :: " + notified);
        }

        return notified;
    }
}